//新浪微博接口
var Config = require("../config");
var KOC = require("koc.common.utils");
var _Interface_WeiBo = module.exports = {
    _RedirectURI: Config.Common.Virtual_Path.Main + "oauth/weibo.token",
    //取得登录地址
    /////////////////////////////////////////
    OAuth_LoginUrl: function () {
        return "https://api.weibo.com/oauth2/authorize?" +
            "&client_id=" + Config.Interface.Weibo.WB_AKEY +
            "&response_type=code" +
            "&redirect_uri=" + _Interface_WeiBo._RedirectURI
            ;
    },
    /////////////////////////////////////////
    //获取AccessToken
    /////////////////////////////////////////
    OAuth_Token: function (code, callback) {
        if (typeof callback !== "function") {
            return;
        }
        var options = {
            url: "https://api.weibo.com/oauth2/access_token",
            form: {
                grant_type: "authorization_code",
                client_id: Config.Interface.Weibo.WB_AKEY,
                client_secret: Config.Interface.Weibo.WB_SKEY,
                code: code,
                redirect_uri: _Interface_WeiBo._RedirectURI
            }
        };
        KOC.Request.post(options, function (err, httpResponse, body) {
            if (err) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: err.message
                }));
                return;
            }
            body = JSON.parse(body);
            if (!body.access_token) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: "error_description:" + body.error_description + "|error_code:" + body.error_code + "|error:" + body.error
                }));
                return;
            }
            callback(KOC.ReturnValue.New({
                message: "",
                returnObject: {
                    Access_Token: body.access_token,
                    Expires_In: body.expires_in
                }
            }));
        });
    },
    /////////////////////////////////////////
    //获取UID
    /////////////////////////////////////////
    OAuth_UID: function (token, callback) {
        if (typeof callback !== "function") {
            return;
        }
        var options = {
            uri: "https://api.weibo.com/2/account/get_uid.json",
            method: "GET",
            qs: {
                access_token: token
            }
        };
        KOC.Request(options, function (error, response, result) {
            if (error) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: error.message
                }));
                return;
            }
            result = JSON.parse(result);
            if (!result.uid) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: "error_description:" + result.error_description + "|error_code:" + result.error_code + "|error:" + result.error
                }));
                return;
            }
            callback(KOC.ReturnValue.New({
                message: "",
                returnObject: {
                    UID: result.uid
                }
            }));
        });
    },
    /////////////////////////////////////////
    //获取用户信息
    /////////////////////////////////////////
    Get_User_Info: function (token, uid, callback) {
        var options = {
            uri: "https://api.weibo.com/2/users/show.json",
            method: "GET",
            qs: {
                access_token: token,
                uid: uid
            }
        };
        KOC.Request(options, function (error, response, result) {
            if (error) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: error.message
                }));
                return;
            }
            result = JSON.parse(result);
            if (!result) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: "request:" + result.request + "|error_code:" + result.error_code + "|error:" + result.error
                }));
                return;
            }
            callback(KOC.ReturnValue.New({
                message: "",
                returnObject: result
            }));
        });
    }
};